package config

import (
	"fmt"

	"github.com/larspensjo/config"
)

type Config struct {
	DBPort   int
	DBHost   string
	DBUser   string
	DBPasswd string
	DB       string

	HTTPPort      int
	HTTPHost      string
	AgentPort     int
	HagroupDBPort int
	InnodbWeight  float64
	AESSalt       string

	Img_Ref          string
	Network_uuid     string
	Network_Name     string
	Default_Root_Pwd string
	Host_Keystone    string
	Host_Nova        string
	Host_Neutron     string
	Jdos_User        string
	Jdos_Pwd         string

	SlaveIoRunning      string
	SlaveSqlRunning     string
	SecondsBehindMaster int
	ProcessStatus       int
	TimeoutUpdate       int

	ChargeUrl       string
	ChargeIdentity  string
	ChargeAccesskey string
	LocationService string
}

func LoadConfig(configFilePath string) (*Config, error) {
	apiConfig := new(Config)

	cfg, err := config.ReadDefault(configFilePath)
	if err != nil {
		return nil, fmt.Errorf("Fail to find config, path: %s err: %s", configFilePath, err)
	}

	if cfg.HasSection("default") {
		apiConfig.DBHost, _ = cfg.String("default", "DBHost")
		apiConfig.DBPort, _ = cfg.Int("default", "DBPort")
		apiConfig.DBUser, _ = cfg.String("default", "DBUser")
		apiConfig.DBPasswd, _ = cfg.String("default", "DBPasswd")
		apiConfig.DB, _ = cfg.String("default", "DBName")

		apiConfig.HTTPHost, _ = cfg.String("default", "HTTPHost")
		apiConfig.HTTPPort, _ = cfg.Int("default", "HTTPPort")
		apiConfig.AgentPort, _ = cfg.Int("default", "AgentPort")
		apiConfig.InnodbWeight, _ = cfg.Float("default", "InnodbSizeWeight")
		apiConfig.AESSalt, _ = cfg.String("default", "AesSalt")

		apiConfig.Img_Ref, _ = cfg.String("default", "Img_Ref")
		apiConfig.Network_uuid, _ = cfg.String("default", "Network_uuid")
		apiConfig.Network_Name, _ = cfg.String("default", "Network_Name")
		apiConfig.Default_Root_Pwd, _ = cfg.String("default", "Default_Root_Pwd")
		apiConfig.Host_Keystone, _ = cfg.String("default", "Host_Keystone")
		apiConfig.Host_Nova, _ = cfg.String("default", "Host_Nova")
		apiConfig.Host_Neutron, _ = cfg.String("default", "Host_Neutron")
		apiConfig.Jdos_User, _ = cfg.String("default", "Jdos_User")
		apiConfig.Jdos_Pwd, _ = cfg.String("default", "Jdos_Pwd")

		apiConfig.SlaveIoRunning, _ = cfg.String("default", "SlaveIoRunning")
		apiConfig.SlaveSqlRunning, _ = cfg.String("default", "SlaveSqlRunning")
		apiConfig.SecondsBehindMaster, _ = cfg.Int("default", "SecondsBehindMaster")
		apiConfig.TimeoutUpdate, _ = cfg.Int("default", "TimeoutUpdate")

		apiConfig.ChargeUrl, _ = cfg.String("default", "ChargeUrl")
		apiConfig.ChargeIdentity, _ = cfg.String("default", "ChargeIdentity")
		apiConfig.ChargeAccesskey, _ = cfg.String("default", "ChargeAccesskey")
		apiConfig.LocationService, _ = cfg.String("default", "LocationService")

		apiConfig.HagroupDBPort, _ = cfg.Int("default", "HagroupDBPort")
		apiConfig.ProcessStatus, _ = cfg.Int("default", "ProcessStatus")
	}

	fmt.Println(apiConfig)

	return apiConfig, nil
}
